package com.gestionabsences.beans;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 *
 * @author matt
 */
public class ResultRequest {
    
    public class ResultTuple {	
	private ArrayList<String> line;
	
	public ResultTuple(ArrayList<String> l){
	    this.line = l;
	}
	
	public ArrayList<String> getLine() {
	    return line;
	}
    }
    
    private boolean resultatPresent = false;
    private LinkedList<String> columnsName;
    private ArrayList<ResultTuple> tuples;
	    
    public ResultRequest(ResultSet resSQLRequest, Object... columns) throws Exception {
	columnsName = new LinkedList<String>();
	tuples = new ArrayList<ResultTuple>();
	
	for ( int i = 0; i < columns.length; i++ ) {
	    columnsName.add(i, (String) columns[i]);
	}
	try {
	    while ( resSQLRequest.next() ) {
		resultatPresent = true;
		
		ArrayList<String> line = new ArrayList<String>();
		for (int i = 0; i < columns.length; i++) {
		    String temp = resSQLRequest.getString((String) columns[i]);
		    line.add(temp);
		    //System.out.println(columns[i] + " has been read et i = " + i + " et vaut " + temp);
		}
		
		tuples.add( new ResultTuple(line) );
	    }
	} catch (SQLException ex) {
	    throw new Exception( ex.getMessage() );
	}
    }
    
    public LinkedList<String> getColumnsName() {
	return columnsName;
    }
    
    public ArrayList<ResultTuple> getTuples() {
	return tuples;
    }
    
    public boolean isResultatPresent() {
	return resultatPresent;
    }
}
